﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using Microsoft.EntityFrameworkCore;
using Mojito.Data;
using Mojito.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mojito.DI
{
    public class DefaultModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var assemblys = AssemblyUtil.GetAllAssemblies().ToArray();
            var baseType = typeof(IDependency);

            // 普通注册
            builder.RegisterType<MainDbContext>()
               .As<DbContext>()
               .InstancePerLifetimeScope()
               .PropertiesAutowired();

            // 注册程序集
            builder.RegisterAssemblyTypes(assemblys.ToArray())
                .Where(t => baseType.IsAssignableFrom(t) && t != baseType)
                .InstancePerLifetimeScope()
                .PropertiesAutowired();
        }
    }
}
